//
//  HNSalesManCycleDetailDateView.m
//  优学荟管理系统
//
//  Created by edz on 2017/11/7.
//  Copyright © 2017年 星道三好. All rights reserved.
//

#import "HNSalesManCycleDetailDateView.h"

@interface HNSalesManCycleDetailDateView ()<UIPickerViewDelegate, UIPickerViewDataSource>
@property (weak, nonatomic) IBOutlet UIView *_ContentView;
/** 时间选择器 */
@property (weak, nonatomic) IBOutlet UIPickerView *PickerView;
/** 最小年份 */
@property (nonatomic, assign) NSInteger Min;
/** 最大年份 */
@property (nonatomic, assign) NSInteger Max;
/** 确定 */
@property (weak, nonatomic) IBOutlet UIButton *SureBtn;
/** 标题 */
@property (weak, nonatomic) IBOutlet UILabel *Title;

/** 年 */
@property (nonatomic, strong) NSArray *YearArr;
/** 月 */
@property (nonatomic, strong) NSArray *MouthArr;
@property (nonatomic, copy) void (^Complete)(NSString *DateStr);

@end
@implementation HNSalesManCycleDetailDateView


/** 实例化 如果最小年大于最大年、最小年为0、最大年为0的条件满足之一都使用默认，1970-2050 */
- (instancetype)initWithCurrentDate:(NSString *)CurrentDate Title:(NSString *)Title Min:(NSInteger)Min Max:(NSInteger)Max Complete:(void (^)(NSString *DateStr))Complete {
    
    self = [[NSBundle mainBundle] loadNibNamed:[HNSalesManCycleDetailDateView description] owner:nil options:nil][0];
    if (self) {
        
        self._ContentView.layer.shadowColor = [UIColor blackColor].CGColor;//shadowColor阴影颜色
        self._ContentView.layer.shadowOffset = CGSizeMake(0,0); //shadowOffset阴影偏移,x向右偏移4，y向下偏移4，默认(0, -3),这个跟shadowRadius配合使用
        self._ContentView.layer.shadowOpacity = 0.8;//阴影透明度，默认0
        self._ContentView.layer.shadowRadius = 4;//阴影半径，默认3
        self.Min = Min;
        self.Max = Max;
        self.PickerView.delegate = self;
        self.PickerView.dataSource = self;
        self.SureBtn.layer.cornerRadius = 8.f;
        self.SureBtn.layer.masksToBounds = YES;
        self.Complete = Complete;
        if (Title != nil && ![Title isEqualToString:@""]) {
            self.Title.text = Title;
        }
        // 自动跳转
        if (CurrentDate != nil && ![CurrentDate isEqualToString:@""]) {
            [self MgrCurrentDate:CurrentDate Complete:^(NSString *Year, NSString *Mouth) {
               
                // 年
                [self.YearArr enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    if ([obj isEqualToString:Year]) {
                        [self.PickerView selectRow:idx inComponent:0 animated:YES];
                        *stop = YES;
                    }
                }];
                
                // 月
                [self.MouthArr enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    if ([obj isEqualToString:Mouth]) {
                        [self.PickerView selectRow:idx inComponent:1 animated:YES];
                        *stop = YES;
                    }
                }];
            }];
        }
        self.frame = [UIScreen mainScreen].bounds;
    }
    return self;
}



#pragma mark - pckerView代理
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    
    return component == 0 ? self.YearArr.count:self.MouthArr.count;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    
    UILabel *Label = (UILabel *)view;
    if (!Label) {
        
        Label = [[UILabel alloc] init];
        Label.textColor = [UIColor blackColor];
        Label.textAlignment = NSTextAlignmentCenter;
        Label.textColor = ColorRGB(99, 201, 149);
    }
    
    if (component == 0) {
        
        Label.text = self.YearArr[row];
    } else {
        
        Label.text = self.MouthArr[row];
    }
    
    return Label;
}

#pragma mark - Action
- (void)MgrCurrentDate:(NSString *)CurrentDate Complete:(void (^)(NSString *Year, NSString *Mouth))Complete {

    CurrentDate = [CurrentDate stringByReplacingOccurrencesOfString:@"月" withString:@""];
    NSArray *Arr = [CurrentDate componentsSeparatedByString:@"年"];
    Complete ? Complete ([NSString stringWithFormat:@"%@年",[Arr firstObject]], [NSString stringWithFormat:@"%@月",[Arr lastObject]]):nil;
}

- (IBAction)Close:(UIButton *)sender {
    
    [self removeFromSuperview];
}

- (IBAction)Sure:(UIButton *)sender {
    
    NSString *YearStr = self.YearArr[[self.PickerView selectedRowInComponent:0]];
    NSString *MouthStr = self.MouthArr[[self.PickerView selectedRowInComponent:1]];
    self.Complete ? self.Complete([NSString stringWithFormat:@"%@%@",YearStr,MouthStr]):nil;
    [self Close:nil];
}
#pragma mark - setter
- (NSArray *)YearArr {
    
    if (!_YearArr) {
        
        // 如果最小年大于最大年、最小年为0、最大年为0的条件满足之一都使用默认，1970-2050
        if (self.Min > self.Max || self.Min == 0 || self.Max == 0) {
            
            self.Min = 1970;
            self.Max = 2050;
        }
        // 添加
        NSMutableArray *Arr = [NSMutableArray array];
        for (NSInteger i = self.Min; i <= self.Max; i ++) {
            [Arr addObject:[NSString stringWithFormat:@"%ld年",i]];
        }
        _YearArr = [Arr copy];
     }
    return _YearArr;
}

- (NSArray *)MouthArr {
    
    if (!_MouthArr) {
        
        _MouthArr = @[@"01月", @"02月", @"03月", @"04月", @"05月", @"06月", @"07月", @"08月", @"09月", @"10月", @"11月", @"12月"];
    }
    return _MouthArr;
}
@end
